package com.hd.trans.files;

import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.IdRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import com.hd.trans.R;
import com.hd.trans.utils.DisplayUtils;

import java.util.Objects;

abstract public class BaseDialogFragment extends DialogFragment {

    protected View mRootView;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//        ThemeUtil.setBaseTheme(getContext(), SdkInitManager.getInstance().getSdkConfiguration().getStyleId());
        Objects.requireNonNull(getDialog()).setCanceledOnTouchOutside(true);
        mRootView = inflater.inflate(getLayoutId(), container, false);
        initWindow();
        return mRootView;
    }

    protected void initWindow() {
        final Window window = Objects.requireNonNull(getDialog()).getWindow();
        window.setBackgroundDrawableResource(R.color.transparent);
        window.getDecorView().setPadding(getPaddingStart(), 0, getPaddingEnd(), 0);
        WindowManager.LayoutParams wlp = window.getAttributes();
        wlp.width = WindowManager.LayoutParams.MATCH_PARENT;
        wlp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        wlp.gravity = getGravity();
//        wlp.windowAnimations = R.style.take_file_anim;
        window.setAttributes(wlp);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
    }

    protected int getGravity() {
        return Gravity.CENTER;
    }

    protected int getPaddingStart() {
        return DisplayUtils.dp2px(requireContext(), 0);
    }

    protected int getPaddingEnd() {
        return DisplayUtils.dp2px(requireContext(), 0);
    }

    protected <T extends View> T getView(@IdRes int id) {
        return mRootView.findViewById(id);
    }

    abstract int getLayoutId();

    abstract void initView();
}
